from flask import Blueprint,jsonify,request
import json
from dbs import dbs
import math
import time

bp_news = Blueprint('news',__name__)


@bp_news.route('/news',methods=['GET'])
def news():
    if request.method == 'GET':
        page = int(request.args.get('page'))
        page_sizt = 2
        start = (page-1)*page_sizt
        sql = 'select * from news limit %d,%d'%(start,page_sizt)
        res = dbs.findall(sql)
        sql2 = 'select count(id) from news'
        res2 = dbs.findone(sql2)
        total = math.ceil(int(res2['count(id)'])/page_sizt)
        return jsonify({'code':200,'list':res,'total':total})


@bp_news.route('/users',methods=['POST'])
def users():
    if request.method == 'POST':
        data = request.data
        if data:
            data = json.loads(data)
        else:
            data = request.form

        sql = 'select * from users where username = "%s"'%(data['username'])
        res = dbs.findone(sql)
        if res:
            if data['password'] == res['password']:
                return jsonify({'code':200,'userid':res['id']})
            else:
                return jsonify({'code':20011,'mes':'密码错误'})
        else:
            return jsonify({'code':20012,'mes':'用户不存在'})


@bp_news.route('/com',methods=['POST','GET'])
def com():
    if request.method == 'POST':
        data = request.data
        if data:
            data = json.loads(data)
        else:
            data = request.form
        sql = 'insert into comments(userid,comid,comment) values(%d,%d,"%s")'%(int(data['userid']),int(data['id']),data['comment'])
        dbs.update(sql)
        sql2 = 'select total_comment from news where id = %d'%(int(data['id']))
        res2 = dbs.findone(sql2)
        count = int(res2['total_comment'])
        count+=1
        sql3 = 'update news set total_comment = %d where id = %d '%(count,int(data['id']))
        dbs.update(sql3)
        return jsonify({'code':200})
    elif request.method == 'GET':
        id = int(request.args.get('id'))
        userid = int(request.args.get('userid'))
        sql = 'select * from user_news where userid = %d and comid = %d'%(userid,id)
        res = dbs.findone(sql)
        if res:
            nowtime = time.time()
            if float(nowtime)-float(res['create_time']) >3600:
                sql1 = 'select title,content from news where id = %d' % (
                    id)
                res1 = dbs.findone(sql1)
                sql3 = 'select comment from comments where comid = %d'%(id)
                res3 = dbs.findall(sql3)
                res1['comment'] = res3
                sql2 = 'update user_news set create_time = "%s" where userid = %d and comid = %d'%(nowtime,int(userid),int(id))
                dbs.update(sql2)
                return jsonify({'code':200,'list':res1})
            else:
                return jsonify({'code':20013,'mes':'一小时只能看一次'})
        else:
            sql5 = 'select title,content from news where id = %d' % (
                id)
            res5 = dbs.findone(sql5)
            sql6 = 'select comment from comments where comid = %d' % (id)
            res6 = dbs.findall(sql6)
            print(res6)
            res5['comment'] = res6
            create_time = time.time()
            sql4 = 'insert into user_news(userid,comid,create_time) values(%d,%d,"%s")'%(int(userid),int(id),create_time)
            dbs.update(sql4)
            return jsonify({'code':200,'list':res5})

